#ifndef __DESFIRE_H
#define __DESFIRE_H
#include <string.h>
#include "include/zkrd522.h"
#include "include/spi.h"
#include "include/delay.h"
#include "include/ca51f003_config.h"	
//#include "include/uart.h"
#include "mifare.h"
#include "des.h"


#define DEBUG 0

//命令数据缓存结构体，用来保存发送的
//命令和数据，然后保存返回接收的数据
typedef struct
{
	unsigned short length;		//命令长度
	unsigned char buff[64];	//命令缓冲
}xdata pcd_cmd_t;

typedef struct
{
	uint8_t moudle_hard[7];
	uint8_t moudle_soft[7];
	uint8_t moudle[14];
}idata moudle_in;

#define ADDR 0X01

#define DESFIRE_VER_KEY 0X0A
#define DESFIRE_SEL_APP 0X5A
#define DESFIRE_CREATE_APP 0XCA
#define DESFIRE_CREATESTD_FILE 0XCD
#define DESFIRE_WRITE_DATA 0X3D
#define DESFIRE_READ_DATA 0XBD
#define DESFIRE_VALUE_FILE 0XCC
#define DESFIRE_BACKUPDATA_FILE 0XCB
#define DESFIRE_LINEARRECORD_FILE 0XC1
#define DESFIRE_CYCLICCORD_FILE 0XC0
#define DESFIRE_DELETE_FILE 0XDF
#define DESFIRE_GETVALUE 0X6C
#define DESFIRE_CREDI 0X0C
#define DESFIRE_DEBIT 0XDC
#define DESFIRE_WRITE_RECORD 0X3B
#define DESFIRE_READRECORD_FILE 0XBB
#define DESFIRE_PICCFO 0XFC
#define DESFIRE_CHANGE_KEY 0XC4

void pcb_init();
void GetKeySettings();
void DES_PPS();
int8_t Read_Cord_Data(uint8_t type,uint8_t file_no,uint8_t *offset,uint8_t *length,uint8_t command);
int8_t Create_Cyclic_LinearRecordFile(uint8_t type,uint8_t file_no,uint8_t *size,uint8_t *num);
int8_t Create_Std_Backup_DataFile(uint8_t type,uint8_t file_no,uint8_t *size);
int8_t des_moudle_in(moudle_in *buf); //读模块信息
int8_t GetApplicationIDs(uint8_t *buf,uint8_t *len);
int8_t FormatPICC();
int8_t des_cmd_transceive(pcd_cmd_t *pcd_cmd);
int8_t desfire_keyVer(uint8_t *key,uint8_t *pkey,uint8_t key_no);
int8_t des_select_app(uint8_t *aid);
int8_t des_create_app(uint8_t *aid,uint8_t lev,uint8_t key_num);
int8_t GetKeyVersion(uint8_t key_no);
int8_t CreateValueFile(uint8_t file_no,uint8_t *lower,uint8_t *upper,uint8_t *value);
int8_t DeleteFile(uint8_t file_no);
int8_t GetValue(uint8_t file_no,int *da);
int8_t Credit_Debit(uint8_t file_no,uint8_t flag,uint8_t *data_buf);
int8_t CommitTransaction(void);
int8_t ClearRecordFile(uint8_t file_no);
int8_t ChangeKey(uint8_t *new_key,uint8_t *d_key);
int8_t ChangeKeySettings(uint8_t key_set,uint8_t *d_key);
int8_t GetFileSettings(uint8_t file_no,uint8_t *file_type);
int8_t DES_transmission(uint8_t *length,uint8_t *buf);
int8_t desocd_transmission(uint8_t *length,uint8_t *buf);
int8_t DES_SBLOCK();
int8_t  write_std_Linear(uint8_t type,uint8_t file_no,uint8_t *offset,uint8_t *length,uint8_t *data_buf);
void uart_rec_buf(uint8_t status,uint8_t command,uint8_t *buf,uint8_t len);
#endif